#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e6+10;
int n,fa[MAXN*2],siz[MAXN*2];
struct node
{
	int a,b,k;
}a[MAXN];
vector<int>v;
map<int,int>m;
int getfa(int x)
{
	return fa[x]==x?x:getfa(fa[x]);
}
void link(int u,int v)
{
	if(siz[u]>siz[v])
	{
		fa[v]=u;
		siz[u]+=siz[v];
	}
	else 
	{
		fa[u]=v;
		siz[v]+=siz[u];
	}
	return;
}
int main()
{
	int T;
	scanf("%d",&T);
	L1:while(T--)
	{
		memset(a,0,sizeof(a));
		scanf("%d",&n);
		for(int i=1;i<=n;++i)
		{
			scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].k);
			v.push_back(a[i].a);
			v.push_back(a[i].b);
		}
		sort(v.begin(),v.end());
		v.resize(unique(v.begin(),v.end())-v.begin());
		for(int i=0;i<v.size();++i)
		{
			m[v[i]]=i;
			fa[i]=i;
			siz[i]=1;
		}
		for(int i=1;i<=n;++i)
		{
			if(!a[i].k)continue;
			int u=getfa(m[a[i].a]),v=getfa(m[a[i].b]);
			if(u==v)continue;
			link(u,v);
		}
		for(int i=1;i<=n;++i)
		{
			if(a[i].k)continue;
			int u=getfa(m[a[i].a]),v=getfa(m[a[i].b]);
			if(u==v)
			{
				printf("NO\n");
				goto L1;
			}
		}
		printf("YES\n");         
	}
	return 0;
}
